package org.youyun56.gpsserver.command.console;

import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSession;
import org.youyun56.gpsserver.command.CommandAssigner;
import org.youyun56.gpsserver.command.GlobalProperties;
import org.youyun56.gpsserver.common.DateUtils;
import org.youyun56.gpsserver.common.MainEngine;
import org.youyun56.gpsserver.common.SessionKey;
import org.youyun56.gpsserver.common.SessionManager;
import org.youyun56.gpsserver.entity.Car;
import org.youyun56.gpsserver.net.GpsServer;
import org.youyun56.gpsserver.utils.GpsServerUtils;

public class ConsoleCommandExecutor extends CommandExecutor {
    private final static Logger log = Logger.getLogger(ConsoleCommandExecutor.class);

    public String reloadCommand(ConsoleCommand sc) {
        CommandAssigner.getInstance().clearCommand();
        try {
            MainEngine.instance.getServer().loadCommands();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public String help(ConsoleCommand sc) {
        StringBuilder sb = new StringBuilder();
        for (ConsoleCommand command : CommandAssigner.getInstance().getAllCommands()) {
            sb.append(command.getStyle()).append(GlobalProperties.NEWLINE);
        }
        return sb.toString();
    }

    public String onlineNum(ConsoleCommand sc) {
        return String.valueOf(GpsServerUtils.online());
    }

    public String onlineInfo(ConsoleCommand sc) {
        StringBuffer sb = new StringBuffer("Total: ").append(GpsServerUtils.online()).append(GlobalProperties.NEWLINE);
        for (IoSession ioSession : GpsServer.acceptor.getManagedSessions().values()) {
            Car car = (Car) ioSession.getAttribute(SessionKey.CAR);
            NioSession session = (NioSession) ioSession;
            sb.append(String.format("SID : %d,Alive : %s, CreateTime : %s IP: %s Mobile: %s", ioSession.getId(),
                    String.valueOf(!session.getCloseFuture().isClosed()), DateUtils.formatFull(new Date(ioSession.getCreationTime())),
                    ioSession.getRemoteAddress(), car != null ? car.getCarMobile() : ""));
            sb.append(GlobalProperties.NEWLINE);
        }
        return sb.toString();
    }

    public String kick(ConsoleCommand sc) {
        for (IoSession ioSession : GpsServer.acceptor.getManagedSessions().values()) {
            Car car = (Car) ioSession.getAttribute(SessionKey.CAR);
            if (car != null) {
                if (car.getCarMobile().equals(sc.getInput().split(" ")[1].trim())) {
                    SessionManager.removeSession(ioSession);
                    ioSession.close(true);
                    return "find and kick successful";
                }
            }
        }
        return "not online";
    }

    public String shutdown(ConsoleCommand sc) {
        System.exit(0);
        return "DONE";
    }

}
